(function($){

    SocketEvent={
        tasks:{},
        eventDaemon:function(e){
            var d=e.substr(0,5);
            switch (d){
            case 'open:':
            case 'close:':
                break;
            case 'send:':
                HttpSocket.send(d.substr(5));
                break;
            case 'data:':
                ;}
            ;},
        eventTrigger:function(e){
            var d=e.substr(0,5);
            if (d=='open:' || d=='close:'){
                localStorage.WSEVENT=d;}
                
            else{
                
                ;}
            ;},
        bind:function(e){
            //PID+
            ;}
    }

    function httpsocket(url){
        if (HttpSocket){return HttpSocket;}
        HttpSocket={
            };
        HttpSocket.tasks={};
        if (ISMASTER){
            HttpSocket.send=function(d){
                HttpSocket.socket.send(d);
                ;};
            HttpSocket.socket=new WebSocket(url);
            HttpSocket.socket.onopen=function (){
                SocketEvent.trigger('open:');};
            HttpSocket.socket.onclose=function(){
                SocketEvent.trigger('close:');};
            HttpSocket.socket.onmessage=function(e){
                //收到的數據格式TID:JSON返回數據
                SocketEvent.trigger(e.data);};
            ;}
        else{
            HttpSocket.send=function(d){
                SocketEvent.eventTrigger('send:'+PID+'_'+d);},
            
            ;}
        ;}
    

    WsHttp={
        tasks:{},
        Socket:null,//HttpSocket(),
        init:function(url){
            WsHttp.Socket=httpsocket(url);
            ;},
        get:function(url,data,onmessage,onerror){
            //data需要發送的數據，ondata傳回數據時回調的函數，onerror發生錯誤時回調函數
            var tid=String(new Date().getTime() % 31536000);
            var s=tid+':'+url+'?'+JSON.stringify(data);
            WsHttp.tasks[tid]=[onmessage,onerror];
            WsHttp.Socket.send(s);
            ;},
        onmessage:function(ata){
            ;},
        onclose:function(data){
            ;},
        doevent:function(data){
            ;}
    }

})(jQuery);